//
// Created by xili on 2024/8/3 17:04.
// Go big or go home.
//
#include <vector>

using namespace std;

class Solution {
public:
    int countBeautifulPairs(vector<int> &nums) {
        int n = nums.size();
        int ans = 0;
        for (int i = 0; i < n - 1; i++) {
            int first = 0;
            int val = nums[i];
            while (val) {
                first = val % 10;
                val /= 10;
            }
            for (int j = i + 1; j < n; j++) {
                int last = nums[j] % 10;
                if (gcd(first, last) == 1) {
                    ans++;
                }
            }
        }
        return ans;
    }

    int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
};